package com.c2soft.nepalcmx.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.log4j.Log4j2;

import org.apache.log4j.Category;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.c2soft.nepalcmx.command.AccountDetailCommand;
import com.c2soft.nepalcmx.command.BrokerFormCommand;
import com.c2soft.nepalcmx.command.TradeBookCommand;
import com.c2soft.nepalcmx.services.BrokerAccountDetailService;
import com.c2soft.nepalcmx.services.TradeBookService;
import com.c2soft.nepalcmx.util.JsonUtil;

@Log4j2
@Controller
@RequestMapping("/broker")
public class BrokerAccountDetailController {
	
	@Autowired
	private BrokerAccountDetailService brokerAccountDetailService;
	
@RequestMapping(value = "/brokerAccountDetail",method=RequestMethod.GET, headers = "Accept=application/json")
	public @ResponseBody List<AccountDetailCommand> getAccountDetail(@RequestParam("brokerId") String brokerId) {	
		List<AccountDetailCommand> accountDetail=brokerAccountDetailService.getBrokerAccounts(brokerId);
		return accountDetail;
	}

@RequestMapping(value = "/accountBrokerDetail",method=RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody List<AccountDetailCommand> getBAccountDetail() {	
	List<AccountDetailCommand> accountDetail=brokerAccountDetailService.getBrokersAccount();
	return accountDetail;
}

@RequestMapping(value = "/AccountDetailX",method=RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody List<AccountDetailCommand> AccountDetailX() {	
	List<AccountDetailCommand> accountDetail=brokerAccountDetailService.getExchangeAccounts();
	return accountDetail;
}

	@RequestMapping(value = "/updateMngUser",method={RequestMethod.POST,RequestMethod.GET}, headers = "Accept=application/json")

	public @ResponseBody List<AccountDetailCommand> updateUserData(HttpServletRequest req,@RequestBody String brokerId) {
		List<AccountDetailCommand> users=brokerAccountDetailService.getBrokerDetail(brokerId);
		return users;
	}
	
	@RequestMapping(value = "/updateAccount", method=RequestMethod.POST, headers = "Accept=application/json")
	@ResponseBody
	public String updateAccount( @RequestBody String json) {
		AccountDetailCommand account= (AccountDetailCommand) JsonUtil.fromJson(json,AccountDetailCommand.class);
        brokerAccountDetailService.updateAccountService(account);
		return "{\"msg\":\"Account Details Updated successfully.\"}";
	}
	
	@RequestMapping(value = "/deleteBAccount", method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String deleteBAccount( HttpServletRequest req) {
		String brokerid=req.getParameter("id");
		
		brokerAccountDetailService.deleteBrokerAccount(brokerid);
		return "{\"success\":\"success\"}";
	}
	
	@RequestMapping(value = "/deleteUAccount", method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String deleteAccount( HttpServletRequest req) {
		String userid=req.getParameter("id");
		
		brokerAccountDetailService.deleteUserAccount(userid);
		 return "{\"success\":\"success\"}";
	}
	

}
